package com.teatime.client.image.photomgr;

import java.util.ArrayList;
import java.util.List;

import com.teatime.client.TLog;
import com.teatime.client.image.photomgr.PhotoMgrView.PhotoInfo;
import com.teatime.client.view.DragGridView;
import com.teatime.client.view.ImageGridAdapter;
import com.teatime.client.view.DragGridView.OnItemDragListener;
import com.teatime.client.view.TPopupWindow;
import com.teatime.client.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class PhotoMgrPopWin extends TPopupWindow 
			implements OnItemDragListener {

	private List<Bitmap> mPhotos;
	private DragGridView mGridView;
	private ImageView    mImgRecycle;
	private int          mImgRecycleWidth;
	private int          mImgRecycleHeight;
	private boolean      mIsImgRecycleBigger;
	private boolean      mHasRM;
	private PhotoMgrView mPhotoMgr;
	
	public PhotoMgrPopWin(Context context, PhotoMgrView photoMgr) {
		super(context);
		mPhotoMgr = photoMgr;
		mImgRecycle = null;
		mIsImgRecycleBigger = false;
		mHasRM = false;
	}
	
	@Override
	protected int getContentLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.view_photo_mgr_pop_win;
	}
	
	@Override
	protected int getMode() {
		// TODO Auto-generated method stub
		return TPopupWindow.MODE_BACK;
	}
	
	@Override
	protected int getTitleId() {
		// TODO Auto-generated method stub
		return -1;
	}
	
	public void open(List<PhotoInfo> photos) {
		
		if (mImgRecycle == null) {
			mImgRecycle = (ImageView)findViewById(R.id.img_photo_mgr_recycle);
		}
		
		if (mPhotos != null) {
			mPhotos.clear();
		}
		else {
			mPhotos = new ArrayList<Bitmap>();
		}
		for (int i = 0; i < photos.size(); i ++) {
			PhotoInfo pi = photos.get(i);
			if (pi != null && pi.bitmap != null) {
				mPhotos.add(pi.bitmap);
			}
		}
		
		mHasRM = false;
		showPhotos();
		
		show();
	}

	protected void showPhotos() {
		if (mGridView == null) {
			mGridView = (DragGridView)findViewById(R.id.photo_grid);
		}
		
		if (mGridView != null) {
			mGridView.setAdapter(new ImageGridAdapter(getContext(), mPhotos));
			mGridView.setOnItemDragListener(this);
		}
		
	}

	private void recyleBigger() {
		if (mImgRecycle == null) {
			return;
		}
		//mImgRecycle.setScaleType(ScaleType.FIT_CENTER);
		ViewGroup.LayoutParams lp = mImgRecycle.getLayoutParams();
		
		mImgRecycleWidth = lp.width;
		mImgRecycleHeight = lp.height;
		lp.width = (int)((float)mImgRecycleWidth * 1.2f);
		lp.height = (int)((float)mImgRecycleHeight * 1.2f);
		mImgRecycle.setLayoutParams(lp);
		mImgRecycle.invalidate();
	}
	
	private void recyleRestore() {
		if (mImgRecycle == null) {
			return;
		}
		//mImgRecycle.setScaleType(ScaleType.CENTER_INSIDE);
		ViewGroup.LayoutParams lp = (RelativeLayout.LayoutParams)mImgRecycle.getLayoutParams();
		lp.width = mImgRecycleWidth;
		lp.height = mImgRecycleHeight;
		mImgRecycle.setLayoutParams(lp);
		
	}
	
	@Override
	public void onDragStart(GridView grid, int dragPosition) {
		// TODO Auto-generated method stub
		//recyleBigger();
		
	}

	@Override
	public void onDrag(GridView grid,
    		int dragPosition, int moveX, int moveY) {
		Rect rect = new Rect(mImgRecycle.getLeft(),
				mImgRecycle.getTop(),
				mImgRecycle.getRight(),
				mImgRecycle.getBottom());
		if (rect.contains(moveX, moveY)) {
			if (!mIsImgRecycleBigger) {
				recyleBigger();
				mIsImgRecycleBigger = true;
			}
		}
		else {
			if (mIsImgRecycleBigger) {
				recyleRestore();
				mIsImgRecycleBigger = false;
			}
		}
	}
	
	@Override
	public void onDragStop(GridView grid, 
			int dragPosition,
			int stopX, int stopY) {
		// TODO Auto-generated method stub
		if (mImgRecycle == null) {
			return;
		}
		Rect rect = new Rect(mImgRecycle.getLeft(),
				mImgRecycle.getTop(),
				mImgRecycle.getRight(),
				mImgRecycle.getBottom());
		if (rect.contains(stopX, stopY)) {
			Bitmap del = mPhotos.get(dragPosition);
			if (del != null) {
				mPhotos.remove(dragPosition);
				mPhotoMgr.delBitmap(del);
			}
			mHasRM = true;
			TLog.v("remove a photo");
		}
		if (mIsImgRecycleBigger) {
			recyleRestore();
			mIsImgRecycleBigger = false;
		}
	}

	@Override
	public void onDismiss() {
		// TODO Auto-generated method stub
		super.onDismiss();
		if (mPhotoMgr != null) {
			mPhotoMgr.onPhotoMgrPopWinClosed(mHasRM, 
					(mGridView == null ? false : mGridView.hasSort()),
					mPhotos);
		}
	}
}
